package com.zjw;

import com.zjw.entity.Student;
import com.zjw.service.GradeService;
import com.zjw.service.StudentService;
import com.zjw.service.impl.GradeServiceImpl;
import com.zjw.service.impl.StudentServiceImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

/**
 * @projectName: studentCourseSelectionManagement
 * @package: com.zjw
 * @className: Test1
 * @author: 翟
 * @date: 2025/4/6 21:11
 */

public class Test1 {
    /**
     * 插入学生记录
     */
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        StudentService studentService = new StudentServiceImpl();
        GradeService gradeService = new GradeServiceImpl();
        do {
            Student student = null;
            gradeService.findAll().forEach(System.out::println);
            System.out.println("请选择学生年级：");
            int gradeId = input.nextInt();
            for (int i = 0; i < gradeService.findAll().size(); i++) {
                if (gradeId == gradeService.findAll().get(i).getGradeId()){
                    System.out.println("请输入学号：");
                    int studentId = input.nextInt();
                    if (studentService.isExistStudentId(studentId)){
                        System.out.println("这个学号已存在");
                        continue;
                    }
                    System.out.println("请输入学生姓名：");
                    String name = input.next().trim();
                    System.out.println("请输入登录密码：");
                    String password = input.next().trim();
                    System.out.println("请输入性别（男/女）：");
                    String sex = input.next().trim();
                    System.out.println("请输入练习电话：");
                    String phone = input.next().trim();
                    System.out.println("请输入地址：");
                    String address = input.next().trim();
                    System.out.println("请输入出生日期：");
                    String birthday = input.next().trim();
                    System.out.println("请输入邮箱：");
                    String email = input.next().trim();
                    System.out.println("请输入身份证号：");
                    String idCard = input.next().trim();
                    student = new Student();
                    student.setGradeId(gradeId);
                    student.setStudentNo(studentId);
                    student.setStudentName(name);
                    student.setLoginPwd(password);
                    student.setAddress(address);
                    try {
                        student.setBornDate(new SimpleDateFormat("yyyy-MM-dd").parse(birthday));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    student.setEmail(email);
                    student.setIdentityCard(idCard);
                    student.setSex(sex.equals("男") ? 0 : 1);
                    student.setPhone(phone);
                    studentService.addStudent(student);
                }
            }
            if (student != null){
                System.out.println("学生信息添加成功");
                System.out.println("是否继续添加学生信息（Y/N）：");
                if (input.next().equalsIgnoreCase("N")){
                    break;
                }
            }else{
                System.out.println("没有这个年级，请重新输入");
            }
        }while (true);
    }
}
